
<!-- 登录弹出框 Start-->
<div class="login" id="login">
    <div class="mask"></div>
    <div class="share-component">
      <div class="popTitle">
        <span class="title">登录账号</span>
        <i class="iconfont close">&#xe600;</i>
      </div>
      <div class="login-box clearfix padd30">
        <div class="hf-input ">
          <input class="hf-username login-name" name="username" placeholder="请输入手机号" data-hf-widgets="1">
          <span class="hf-err-tips login-name-error"> <i class="iconfont icon_error">&#xe605;</i><em></em></span>
        </div>
        <div class="hf-input ">
          <input type="password" class="hf-password login-pwd" name="password" maxlength="20" placeholder="请输入密码" data-hf-widgets="2">
          <span class="hf-err-tips login-pwd-error"> <i class="iconfont icon_error">&#xe605;</i><em></em></span>
        </div>
        <button class="hf-btn btn-bule btn-login" type="submit" >登录</button>
        <div class="hf-third-login clearfix">
          <ul class="forget-pwd ">
            <li class="fl"><a href="#" class="col-blue reg-btn">注册账号</a></li>
            <li class="fr"><a href="#" class="find-pwd">忘记密码</a> </li>
          </ul>
        </div>
      </div>
    </div>
</div>
<!-- 登录弹出框 Start-->
<!-- 注册弹出框 Start-->
<div class="register" id="register">
    <div class="mask"></div>
    <div class="share-component">
      <div class="popTitle">
        <span class="title">注册账号         </span>
        <i class="iconfont close">&#xe600;</i>
      </div>
      <div class="login-box clearfix padd30">
        <div class="hf-input">
          <input class="hf-username reg-mobile" name="mobile" placeholder="请输入手机号" data-hf-widgets="1">
          <span class="hf-err-tips reg-mobile-error"> <i class="iconfont icon_error">&#xe605;</i><em>手机号码格式不正确</em></span>
        </div>
        <div class="hf-input ">
          <input class="hf-captcha reg-code" maxlength="8" name="captcha" data-rule-required="true" placeholder="请输入短信验证码">
          <span class="btn-half flag" data-tag="user-reg">获取验证码</span>
          <span class="hf-err-tips reg-code-error"> <i class="iconfont icon_error">&#xe605;</i><em>密码错误</em></span>
        </div>


        <div class="hf-input ">
          <input class="hf-password reg-name" name="username" maxlength="20" placeholder="请输入姓名" data-hf-widgets="2">
          <span class="hf-err-tips reg-name-error"> <i class="iconfont icon_error">&#xe605;</i><em></em></span>
        </div>

        <div class="hf-input ">
          <input type="password" class="hf-password reg-pwd" name="password" maxlength="20" placeholder="请设置登录密码" data-hf-widgets="2">
          <span class="hf-err-tips reg-error"> <i class="iconfont icon_error">&#xe605;</i><em>密码格式</em></span>
        </div>
        <button class="hf-btn btn-bule btn-register" type="submit" >注册</button>
        <div class="hf-third-login clearfix">
          <ul class="forget-pwd ">
            <li class="fl"><a href="#" class="col-blue"></a></li>
            <li class="fr"><a href="#" class="login-btn"><em>已有账号？</em>立即登录</a> </li>

          </ul>
        </div>
      </div>
    </div>
</div>
<!-- 注册弹出框 Start-->
<!-- 找回密码 Start-->
<div class="resetpwd" id="resetpwd">
    <div class="mask"></div>
    <div class="share-component">
      <div class="popTitle">
        <span class="title">找回密码 </span>
        <i class="iconfont close">&#xe600;</i>
      </div>
      <div class="login-box clearfix padd30">
        <div class="hf-input ">
          <input class="hf-username find_mobile" name="find_mobile" placeholder="请输入手机号" data-hf-widgets="1">
          <span class="hf-err-tips find-mobile-error"> <i class="iconfont icon_error">&#xe605;</i><em>手机号码格式不正确</em></span>
        </div>
        <div class="hf-input ">
          <input class="hf-captcha" maxlength="8" name="captcha" data-rule-required="true" placeholder="请输入短信验证码">
          <span class="btn-half flag " data-tag="fd-pwd">获取验证码</span>
          <span class="hf-err-tips find-code-error"> <i class="iconfont icon_error">&#xe605;</i><em>密码错误提示</em></span>
        </div>
        <div class="hf-input ">
          <input type="password" class="hf-password" name="password" maxlength="20" placeholder="请设置登录密码" data-hf-widgets="2">
          <span class="hf-err-tips find-error"> <i class="iconfont icon_error">&#xe605;</i><em>密码格式错误</em></span>
        </div>
        <button class="hf-btn btn-bule" >下一步</button>
       
      </div>
    </div>
</div>
{insert_scripts files="../../js/zepto.min.js"}

<!-- 找回密码 Start-->
<script type="text/javascript">
  var _arr = Array(), cur_window = '', reg_mobile = '',wait = 60, is_send = false,json_item_info = '',find_mobile='';
  $(function(){
      $('.btn-login').on('click', doLogin);
      $('.reg-btn').on('click', function(e){
           cur_window = 'register';
           $('.login,.resetpwd').hide();
           $('.register').show();         
       });
      $('.login-btn').on('click', function(e){
          $('.login').show();
          $('.register,.resetpwd').hide();
           
      });
      $('.close').on('click', function(e){ 
          cur_window = '';
          is_send = false;
          $('#login,#register,#resetpwd,.class-dialog,.course-ad').hide();
           
      });

      $('.reg-mobile').on('keyup', function(){
          reg_mobile = $('.reg-mobile').val();
          if(validateMobile(reg_mobile)) {
            $('.register').find('.flag').addClass('hf-dtm-get');
          }else $('.register').find('.flag').removeClass('hf-dtm-get');

      })

      $('.find_mobile').on('keyup', function(){
          find_mobile = $('.find_mobile').val();
          if(validateMobile(find_mobile)) {
            $('.resetpwd').find('.flag').addClass('hf-dtm-get');
          }else $('.resetpwd').find('.flag').removeClass('hf-dtm-get');

      })

      $('.find-pwd').on('click', function(e){
          cur_window = 'resetpwd';
          $('.login,.register').hide();
          $('#resetpwd').show();
      });

      $('.register,.resetpwd').on('click', 'span.hf-dtm-get', getCode);
      $('.btn-register').on('click', Register);

  })
   
   


  function getCode(){
      if( !is_send ){
        var mobile ='';
        if( cur_window == 'resetpwd' ) mobile = find_mobile;
        else mobile = reg_mobile;
        $.post('/mobile/user.php?act=send_code', {'phone':mobile}, function(data){
          console.info(data);
            var data = jsonToArray(data);
            console.info(data.status);
            if( data.status != 'ok' ) {
                if( data.type == 'isexist' ){
                    if( cur_window == 'register' ){
                        showError('.reg-mobile-error', '手机号码已经存');
                        return;
                    }
                }else $('.'+cur_window).find('.code-error').text('验证码发送失败').show();
            }else{
                time( $('.'+cur_window).find(".hf-dtm-get" ));
            }
        })
      }

  }

  function Register(){
      var reg_code = $('.reg-code').val();
      var reg_pwd = $('.reg-pwd').val();
      var reg_name = $('.reg-name').val();
      if(!validateMobile(reg_mobile)){
          showError('.reg-mobile-error', '请输入正确的手机号');
          return;
      }else $('.reg-mobile-error').hide();

      if(!reg_name){
          showError('.reg-name-error', '请输入姓名');
          return;
      }else $('.reg-name-error').hide();
      
      if(!reg_pwd){
          showError('.reg-pwd-error', '请输入密码');
          return;
      }else $('.reg-pwd-error').hide();

      if( reg_pwd.length < 6 || reg_pwd.length > 20 ){
          showError('.reg-pwd-error', '密码长度6到20位');
          return;
      }else $('.reg-pwd-error').hide();
      $('.reg-mobile-error,.reg-code-error,.reg-error').hide();
      $.post('/mobile/user.php?act=act_register', {'phone':reg_mobile,'pwd':reg_pwd,'username':reg_name,'code':reg_code,'item':json_item_info}, function(data){
          var reg_data = jsonToArray(data);
          var cur_path = window.location.href;
          if( reg_data.status != 'ok' ) {
              if( reg_data.type == 'isexist' ) {
                  showError('.reg-mobile-error', reg_data.error);
                  return;
              }else if( reg_data.type == 'code_error' ){
                  showError('.reg-code-error', reg_data.error);
                  return;
              }else if( reg_data.type == 'reg_error' ){
                  showError('.reg-error', reg_data.error);
                  return;
              }else{
                  showError('.reg-error', '网络异常，请重新提交');
                  return;
              }
          }else{
            if( cur_path.indexOf("/goods.php") > 0 ){
                location.href="/mobile/order.php?act=order_lise";
            }else{
                location.reload();
            }
          }
      })
  }

  function time( btn ) {
      if( wait == 0 ) {
          btn.text( "重发验证码" );
          wait = 60;
          is_send = false;
      }
      else {
          is_send = true;
          wait--;
          setTimeout( function() {
              time( btn );
          }, 1000 );
          btn.text( wait + "s后重发" );
      }
  }

  function validateMobile( phone ){
    var myreg = /^1[34578][0-9]{9}$/; 
    if(!myreg.test(phone)) return false; 
    else return true;
  }

  function doLogin(){
      var phone = $('.login-name').val(), pwd = $('.login-pwd').val();
      if( !phone ){ 
        showError('.login-name-error','请输入手机号');
        return;
      }

      if(!validateMobile(phone)){
          showError('.login-name-error','请输入正确的手机号');
          return;
      }else $('.login-name-error').hide();

      if( !pwd ){ 
        showError('.login-pwd-error','请输入密码');
        return;
      }else $('.login-pwd-error').hide();
      
      $('.login-name-error,.login-pwd-error').hide();
      $.post('/mobile/user.php?act=do_login', {'mobile':phone,'pwd':pwd,'item':json_item_info}, function(data){

          var login_data = jsonToArray(data);
          var cur_path = window.location.href;
          if( login_data.status == 'ok' ){
            if( cur_path.indexOf("/goods.php") > 0 ){
                location.href="/mobile/order.php?act=order_lise";
            }else{
                location.reload();
            }
              
          }else{
              if( login_data.type == 'no_user' ){
                  showError('.login-name-error', login_data.error);
                  return;
              }else {
                  showError('.login-pwd-error', login_data.error);
                  return;
              }
          }
      })
  }

  function findPwd(){
      find_mobile = $('.find_mobile').val();
      if(!validateMobile(find_mobile)){
          showError('.find-mobile-error', '请输入正确的手机号');
          return;
      }

      $.post('/mobile/user.php?act=find_pwd', {'phone':find_mobile,'pwd':reg_pwd,'item':json_item_info}, function(data){
          var find_data = jsonToArray(data);
          var cur_path = window.location.href;
          if( find_data.status != 'ok' ) {
              if( find_data.type == 'no_user' ) {
                  showError('.find-mobile-error', find_data.error);
                  return;
              }else if( find_data.type == 'code_error' ){
                  showError('.find-code-error', find_data.error);
                  return;
              }else if( find_data.type == 'error' ){
                  showError('.find-error', find_data.error);
                  return;
              }else{
                  showError('.find-error', '网络异常，请重新提交');
                  return;
              }
          }else{
            if( cur_path.indexOf("/goods.php") > 0 ){
                location.href="/mobile/order.php?act=order_lise";
            }else{
                location.reload();
            }
          }
      })
  }

  function jsonToArray( obj ){
      JSON.parse(obj, function (key, value) { _arr[key] = value;});
      return _arr;
  }

  function showError( obj, txt ){
      $(obj).find('em').text(txt);
      $(obj).show(); 
  }

</script>